package com.mapper;

import com.pojo.Likes;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface LikesMapper {

    @Insert("INSERT INTO likes(user_id, note_id, created_at) " +
            "VALUES(#{userId}, #{noteId}, #{createdAt})")
    int insert(Likes like);

    @Delete("DELETE FROM likes WHERE user_id = #{userId} AND note_id = #{noteId}")
    int delete(@Param("userId") Integer userId, @Param("noteId") Integer noteId);

    @Delete("DELETE FROM likes WHERE note_id = #{noteId}")
    int deleteByNoteId(Integer noteId);

    @Delete("DELETE FROM likes WHERE user_id = #{userId}")
    int deleteByUserId(Integer userId);

    @Select("SELECT * FROM likes WHERE user_id = #{userId} AND note_id = #{noteId}")
    Likes selectByUserAndNote(@Param("userId") Integer userId, @Param("noteId") Integer noteId);

    @Select("SELECT * FROM likes WHERE note_id = #{noteId}")
    List<Likes> selectByNoteId(Integer noteId);

    @Select("SELECT * FROM likes WHERE user_id = #{userId}")
    List<Likes> selectByUserId(Integer userId);

    @Select("SELECT COUNT(*) FROM likes WHERE note_id = #{noteId}")
    int countByNoteId(Integer noteId);

    @Select("SELECT COUNT(*) FROM likes WHERE user_id = #{userId}")
    int countByUserId(Integer userId);

    @Select("SELECT COUNT(*) FROM likes WHERE user_id = #{userId} AND note_id = #{noteId}")
    int exists(@Param("userId") Integer userId, @Param("noteId") Integer noteId);
}